package com.lenovo.manufacture;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ProblemVehicles extends AppCompatActivity {
    private Button btReturn;
    private ListView listView1;
    private List<ProblemData1.DataBean> data1 = new ArrayList<>();
    private List<ProblemData.DataBean> data = new ArrayList<>();
    private final Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://hh1.me:5001/Interface/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_problem_vehicles);
        getvehicles();
        initView();
        btReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    private void initView() {
        btReturn=findViewById(R.id.bt_return);
        listView1 = findViewById(R.id.ll_problemcar);
    }

    private void getvehicles() {
        Service service = retrofit.create(Service.class);
        Call<ProblemData1> getallcar = service.getAllCar();
        getallcar.enqueue(new Callback<ProblemData1>() {
            @Override
            public void onResponse(Call<ProblemData1> call, Response<ProblemData1> response) {
                for (ProblemData1.DataBean dataBean : response.body().getData()) {
                    data1.add(dataBean);
                }
                Call<ProblemData> idvehicles = service.isVehicles();
                idvehicles.enqueue(new Callback<ProblemData>() {
                    @Override
                    public void onResponse(Call<ProblemData> call, Response<ProblemData> response) {
                        data.addAll(response.body().getData());
                        MyAdapter myAdapter = new MyAdapter();
                        listView1.setAdapter(myAdapter);
                    }

                    @Override
                    public void onFailure(Call<ProblemData> call, Throwable t) {

                    }
                });
            }

            @Override
            public void onFailure(Call<ProblemData1> call, Throwable t) {

            }
        });
    }

    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return data1.size();

        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = LayoutInflater.from(ProblemVehicles.this).inflate(R.layout.row_three, parent, false);
            TextView tvId = (TextView) convertView.findViewById(R.id.tv_id);
            TextView tvCartype = (TextView) convertView.findViewById(R.id.tv_cartype);
            TextView tvCardescribe = (TextView) convertView.findViewById(R.id.tv_cardescribe);
            TextView tvInformation = (TextView) convertView.findViewById(R.id.tv_information);
            TextView tvMoney = (TextView) convertView.findViewById(R.id.tv_money);

            tvId.setText(data1.get(position).getId()+ "");
            Log.d("TAG", "getView: " + data1.get(position).getCarTypeName());
            tvCartype.setText(data1.get(position).getCarTypeName());
            tvCardescribe.setText(data1.get(position).getContent());
            tvInformation.setText(data.get(0).getUserLineId() + "");
            tvMoney.setText(data1.get(position).getRepairPrice() + "");
            return convertView;
        }

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}